﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using CarFrameWork.Common;
namespace CarFrameWork.Model.CanShuHua
{
	/// <summary>
	/// 类分动器。
	/// </summary>
	[Serializable]
	public partial class 分动器
	{
		public 分动器()
		{}
		#region Model
		private int _id;
		private string _childnode;
		private DateTime _datetime;
		private string _tag;
		/// <summary>
		/// 
		/// </summary>
		public int ID
		{
			set{ _id=value;}
			get{return _id;}
		}
		/// <summary>
		/// 
		/// </summary>
		public string ChildNode
		{
			set{ _childnode=value;}
			get{return _childnode;}
		}
		/// <summary>
		/// 
		/// </summary>
		public DateTime DateTime
		{
			set{ _datetime=value;}
			get{return _datetime;}
		}
		/// <summary>
		/// 
		/// </summary>
		public string tag
		{
			set{ _tag=value;}
			get{return _tag;}
		}
		#endregion Model


		#region  Method

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public 分动器(int ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID,ChildNode,DateTime,tag ");
			strSql.Append(" FROM [分动器] ");
			strSql.Append(" where ID=@ID ");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
			parameters[0].Value = ID;

			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["ID"]!=null && ds.Tables[0].Rows[0]["ID"].ToString()!="")
				{
					this.ID=int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["ChildNode"]!=null && ds.Tables[0].Rows[0]["ChildNode"].ToString()!="")
				{
					this.ChildNode=ds.Tables[0].Rows[0]["ChildNode"].ToString();
				}
				if(ds.Tables[0].Rows[0]["DateTime"]!=null && ds.Tables[0].Rows[0]["DateTime"].ToString()!="")
				{
					this.DateTime=DateTime.Parse(ds.Tables[0].Rows[0]["DateTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["tag"]!=null && ds.Tables[0].Rows[0]["tag"].ToString()!="")
				{
					this.tag=ds.Tables[0].Rows[0]["tag"].ToString();
				}
			}
		}

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{

		return DbHelperSQL.GetMaxID("ID", "分动器"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from [分动器]");
			strSql.Append(" where ID=@ID ");

			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
			parameters[0].Value = ID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}

        /// <summary>
        /// 增加一条空数据
        /// </summary>
        public int AddNull()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [分动器] (");
            strSql.Append("ChildNode,tag)");
            strSql.Append(" values (");
            strSql.Append("@ChildNode,@tag)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@ChildNode", SqlDbType.NVarChar),
					new SqlParameter("@tag", SqlDbType.NVarChar,50)};
            parameters[0].Value = string.Empty;
            parameters[1].Value = string.Empty;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add()
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into [分动器] (");
			strSql.Append("ChildNode,DateTime,tag)");
			strSql.Append(" values (");
			strSql.Append("@ChildNode,@DateTime,@tag)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@ChildNode", SqlDbType.NVarChar),
					new SqlParameter("@DateTime", SqlDbType.DateTime),
					new SqlParameter("@tag", SqlDbType.NVarChar,50)};
			parameters[0].Value = ChildNode;
			parameters[1].Value = DateTime;
			parameters[2].Value = tag;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update()
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update [分动器] set ");
			strSql.Append("ChildNode=@ChildNode,");
			strSql.Append("DateTime=@DateTime,");
			strSql.Append("tag=@tag");
			strSql.Append(" where ID=@ID ");
			SqlParameter[] parameters = {
					new SqlParameter("@ChildNode", SqlDbType.NVarChar),
					new SqlParameter("@DateTime", SqlDbType.DateTime),
					new SqlParameter("@tag", SqlDbType.NVarChar,50),
					new SqlParameter("@ID", SqlDbType.Int,4)};
			parameters[0].Value = ChildNode;
			parameters[1].Value = DateTime;
			parameters[2].Value = tag;
			parameters[3].Value = ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from [分动器] ");
			strSql.Append(" where ID=@ID ");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
			parameters[0].Value = ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public void GetModel(int ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID,ChildNode,DateTime,tag ");
			strSql.Append(" FROM [分动器] ");
			strSql.Append(" where ID=@ID ");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
			parameters[0].Value = ID;

			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["ID"]!=null && ds.Tables[0].Rows[0]["ID"].ToString()!="")
				{
					this.ID=int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["ChildNode"]!=null && ds.Tables[0].Rows[0]["ChildNode"].ToString()!="")
				{
					this.ChildNode=ds.Tables[0].Rows[0]["ChildNode"].ToString();
				}
				if(ds.Tables[0].Rows[0]["DateTime"]!=null && ds.Tables[0].Rows[0]["DateTime"].ToString()!="")
				{
					this.DateTime=DateTime.Parse(ds.Tables[0].Rows[0]["DateTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["tag"]!=null && ds.Tables[0].Rows[0]["tag"].ToString()!="")
				{
					this.tag=ds.Tables[0].Rows[0]["tag"].ToString();
				}
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM [分动器] ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		#endregion  Method
	}
}

